package com.study.talk.ui.adapter

import android.content.Context
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.study.talk.R
import com.study.talk.ui.adapter.base.BaseListAdapter
import com.study.talk.databinding.ImgUriGridItemBinding

class ChooseImageGridAdapter(val itemClick: (Int) -> Unit, val itemLongClick: (Int, View) -> Unit) :
    BaseListAdapter<Uri, ChooseImageGridAdapter.ViewHolder>() {

    class ViewHolder(val context: Context, val binding: ImgUriGridItemBinding) :
        RecyclerView.ViewHolder(binding.root)


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            parent.context, DataBindingUtil.inflate(
                LayoutInflater.from(parent.context), R.layout.img_uri_grid_item, parent, false
            )
        )
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.imageUri = getItem(holder.adapterPosition)
        // 设置图片的长按事件
        holder.binding.root.setOnLongClickListener {
            itemLongClick(holder.adapterPosition, it)
            true
        }
        holder.binding.root.setOnClickListener {
            itemClick(holder.adapterPosition)
        }
    }
}
